﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using WindowsGame.Common.Data;
using WindowsGame.Common.Objects;
using WindowsGame.Common.Static;

namespace WindowsGame.Common.Managers
{
	public interface ISpriteManager
	{
		void LoadContent();
		void LoadContent(SnakeData[] snakeData, BoardType boardType, Int32 wide, Int32 high);
		void Update(GameTime gameTime);
		void Draw();
		void Reset();
	}

	public class SpriteManager : ISpriteManager
	{
		private readonly IList<Sprite> sprites;

		public SpriteManager()
		{
			sprites = new List<Sprite>();
		}

		public void LoadContent()
		{
			SnakeData[] snakeData = MyGame.Manager.ConfigManager.SnakeData;
			BoardType boardType = MyGame.Manager.ConfigManager.BoardType;

			LoadContent(snakeData, boardType, Constants.SCREEN_WIDE, Constants.SCREEN_HIGH);
		}

		public void LoadContent(SnakeData[] snakeData, BoardType boardType, Int32 wide, Int32 high)
		{
			Int32 noCols = wide / (8 * (Int32)boardType);
			Int32 noRows = high / (8 * (Int32)boardType);

			sprites.Clear();
			foreach (SnakeData data in snakeData)
			{
				Sprite sprite = new Sprite(data, boardType, noCols, noRows);
				sprites.Add(sprite);
			}
		}

		
		public void Update(GameTime gameTime)
		{
			foreach (Sprite sprite in sprites)
			{
				sprite.Update(gameTime);
			}
		}

		public void Draw()
		{
			foreach (Sprite sprite in sprites)
			{
				sprite.Draw();
			}
		}

		public void Reset()
		{
			foreach (Sprite sprite in sprites)
			{
				sprite.Reset();
			}
		}

	}
}